package cn.mdmm.controller.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mdmm.common.MessageConstant;
import cn.mdmm.common.SpringSecurityUtils;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.system.MenuTree;
import cn.mdmm.entity.system.SysMenu;
import cn.mdmm.entity.system.SysUser;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.system.SysMenuService;
import cn.modoumama.common.exception.ServiceException;
import cn.modoumama.common.exception.SystemException;
import cn.modoumama.common.utils.JSONUtils;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;

@Controller
@RequestMapping(value = "/system/prg/menu")
public class SysMenuController extends BaseController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SysDictService sysDictService;
	@RequestMapping(value = "init")
	public String init(HttpServletRequest request){
		Map<String,String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusMap", JSONUtils.toJson(statusMap));
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		return "system/menu/init";
	}
	@RequestMapping(value = "list")
	@ResponseBody
	public GridDataModel list(HttpServletRequest request,SysMenu menu) {
		logger.debug("method: list() ");
		Page po = getPage(request,"MENU_LEVEL asc,MENU_ORDER asc");
		po.getCondition().put("pMenuId", menu.getMenuId());
		po.getCondition().put("menuNameLike", menu.getMenuName());
		po.getCondition().put("menuLevel", menu.getMenuLevel());
		po.getCondition().put("menuStatus", menu.getMenuStatus());
		po.getCondition().put("updateTimeStart",request.getParameter("updateTimeStart"));
		po.getCondition().put("updateTimeEnd",request.getParameter("updateTimeEnd"));
		GridDataModel gridDataModel = sysMenuService.getGridDataModelByCondition(po);
		return gridDataModel;
	}
	
	@RequestMapping(value = "showAdd")
	public String showAdd(HttpServletRequest request){
		Long menuPid = Long.valueOf(request.getParameter("menuPid"));
		SysMenu sMenuP = sysMenuService.findById(menuPid);
		request.setAttribute("sMenuP", sMenuP);
		Map<String,String>statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		Map<String,String> menuTypeMap = sysDictService.getDetailValueMap("菜单类型");
		request.setAttribute("menuTypeCombo", JSONUtils.toCombo(menuTypeMap));
		return "system/menu/add";
	}
	
	@RequestMapping(value = "insert")
	public String insert(HttpServletRequest request,SysMenu menu){
		Integer menuPid = Integer.valueOf(request.getParameter("menuPid"));
		Integer menuLevel = Integer.valueOf(menu.getMenuLevel())+1;
		menu.setMenuPid(menuPid);
		menu.setMenuLevel(menuLevel);
		menu.setMenuStatus(1);
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		sysMenuService.insert(menu);
		return "success";
	}
	
	@RequestMapping(value = "showEdit")
	public String showEdit(HttpServletRequest request){
		Long menuId = Long.valueOf(request.getParameter("menuId"));
		SysMenu sMenu = sysMenuService.findById(menuId);
		SysMenu sMenuP = sysMenuService.findById(Long.valueOf(sMenu.getMenuPid()));
		request.setAttribute("sMenu", sMenu);
		request.setAttribute("sMenuP", sMenuP);
		Map<String,String>statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		Map<String,String> menuTypeMap = sysDictService.getDetailValueMap("菜单类型");
		request.setAttribute("menuTypeCombo", JSONUtils.toCombo(menuTypeMap));
		return "system/menu/edit";
	}
	
	@RequestMapping(value = "update")
	public String update(HttpServletRequest request,SysMenu sMenu){
		sMenu.setUpdateTime(new Date());
		sysMenuService.mergeById(sMenu);
		return "success";
	}
	@RequestMapping(value = "delete")
	@ResponseBody
	public ResponseBodyToOne delete(HttpServletRequest request){
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: delete() ");
		try {
			String ids = request.getParameter("ids");
			String[] menuIds = ids.split(",");
			sysMenuService.deleteMenuByIds(menuIds);
		} catch (Exception e) {
			throw new ServiceException(MessageConstant.SYSTEM_ERROR);
		}
		return responseBody;
	}
	
	@RequestMapping(value = "tree")
	@ResponseBody
	public List<MenuTree> tree(HttpServletRequest request){
		List<MenuTree> menuTrees = sysMenuService.listTree(true,false,false);
		return menuTrees;
	}
	
	@RequestMapping(value = "functionTree")
	@ResponseBody
	public List<MenuTree> functionTree(HttpServletRequest request){
		List<MenuTree> menuTrees = sysMenuService.listTree(true,false,true);
		return menuTrees;
	}
	
	@RequestMapping(value = "myFunctionTree")
	@ResponseBody
	public List<MenuTree> myFunctionTree(HttpServletRequest request){
		SysUser user= SpringSecurityUtils.getCurrentUser();
		List<MenuTree> menuTrees = sysMenuService.listUserTree(user.getId(), true,true);
		return menuTrees;
	}
	
	@RequestMapping(value = "checkMenu")
	@ResponseBody
	public ResponseBodyToOne checkMenuName(HttpServletRequest request,SysMenu menu) {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		Map<String, Object> condititon = new HashMap<String, Object>();
		condititon.put("power", menu.getPower());
		condititon.put("notMenuId", menu.getMenuId());
		int flag = sysMenuService.countByCondition(condititon);
		if(flag > 0 ){
			throw new SystemException("不存在");
        }
        return responseBody;
	}
	
	@RequestMapping(value = "getMenu")
	@ResponseBody
	public SysMenu getMenu(HttpServletRequest request){
		Long menuId = Long.valueOf(request.getParameter("menuId"));
		SysMenu sysMenu = sysMenuService.findById(menuId);
		return sysMenu;
	}
}
